const md = require('../../utils/md5.js');
const app = getApp();
const post = require('../../utils/post.js');
var util = require('../../utils/util.js');
let lunnums = 1;
let paying = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    model: 0,
    showtype: '众库钱包',
    sex: 0,
    hide: 1,
    hidecode: 1,
    payzokoo: 0,
    interval:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var payno = options.payno;
    if (payno) {
      this.setData({
        payno: payno
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onUnload: function () {
    paying = 0;
    lunnums = 1;
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  zfb: function () {
    if (paying == 1) {
      return;
    }
    var that = this;
    that.setData({
      zfb: 1
    })
    wx.showLoading({
      title: '正在收款...',
    })
    var money = that.data.money ? that.data.money : 0
    if (money <= 0) {
      wx.showModal({
        title: '',
        content: '输入的金额必须大于0',
      })
      that.setData({
        zfb: 0
      })
    }

    that.start();
  },
  wx: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  getkey: function (e) {
    this.setData({
      money: e.detail.value
    })
  },
  start2: function (paytype = 'AUTO') {
    paying = 1;
    var that = this;
    var datas = {};
    var url = "/payment/scanPayBSC";
    var time = util.formatTime(new Date());
    datas.AccID = wx.getStorageSync('depid');
    time = time.replace(/ /g, "");
    time = time.replace(/:/g, "");
    time = time.replace(/-/g, "");
    time = time.substring(2);
    datas.BVCode = datas.AccID + time;
    console.log(datas.BVCode);
    datas.AuthCode = that.data.payno;
    datas.Money = that.data.money;
    datas.Body = "钱包收款";
    datas.SrcName = "钱包收款";
    datas.Desc = "";
    datas.PsnName = wx.getStorageSync('userinfo').MebName;
    datas.PayType = paytype;
    datas.TradeNo = datas.BVCode;
    console.log(datas);
    // return;
    var datacall = function (res) {
      console.log(res);
      that.setData({
        wx: 0
      })
      if (res.data.code == 0 && res.data.data == 'Y') {
        wx.hideLoading()
        wx.showModal({
          title: '',
          content: '收款成功!',
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })

            } else {
              wx.navigateBack({
                delta: 1
              })
            }
          }

        })
      } else {
        if (res.data.data == "N") {
          that.lunxun(datas.AccID, datas.TradeNo)
        } else {
          wx.hideLoading()
          wx.showModal({
            title: '',
            content: res.data.msg,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              } else {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        }
      }
    }

    var res2 = that.getdata2(url, datas, datacall);
  },
  start: function (paytype = 'AUTO') {
    paying = 1;
    var that = this;
    var datas = {};
    var url = "/pay/orders/wallet";
    var time = util.formatTime(new Date());
    datas.accId = wx.getStorageSync('depid');
    time = time.replace(/ /g, "");
    time = time.replace(/:/g, "");
    time = time.replace(/-/g, "");
    time = time.substring(2);
    datas.posCode = datas.accId + time;
    // console.log(datas.BVCode);
    datas.authCode = that.data.payno;
    datas.orderAmount = that.data.money;
    datas.orderDesc = "钱包收款";
    datas.remark = "";
    console.log(datas);
    // return;
    var datacall = function (res) {
      console.log(res);
      that.setData({
        wx: 0
      })
      if (res.data.code == 0 && res.data.data.orderStatus == '03') {
        wx.hideLoading()
        wx.showModal({
          title: '',
          content: '收款成功!',
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })
            } else {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      } else {
        if (res.data.data.orderStatus == "02") {
          that.data.interval = setInterval(function () {
            that.lunxun(res.data.data.orderNo)
            if (lunnums==16){
              return;
            }
          }, 3000)

        } else {
          wx.hideLoading()
          wx.showModal({
            title: '',
            content: res.data.msg,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              } else {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        }
      }
    }

    var res2 = that.getdata2(url, datas, datacall);
  },
  //通用模板post请求
  getdata2: function (url, data, callback,isjson=0) {
    var that = this;
    var apinameIsMeb = url;
    let headertype ='application/x-www-form-urlencoded';
    if(isjson){
      headertype = 'application/json';
    }
    wx.request({
      //后台接口地址
      url: "https://api.zokoo.cn" + apinameIsMeb,
      data: data,
      method: 'POST',
      header: { 'content-type': headertype },
      success: function (res) {
        console.log(res);
        callback(res);
      },
      fail: function (res) {
        console.log(res)
        wx.showModal({
          title: '',
          content: "error：" + res.msg,
        })
        return;
      }
    })
  },
  lunxun: function (tradeno) {
    lunnums++;
    var that = this;
    var data = {};
    data.orderNo = tradeno;
    data.isNotify=true;
    if (lunnums > 15) {
      wx.hideLoading()
      clearInterval(that.data.interval);
      wx.showModal({
        title: '',
        content: '暂无结果，请去账单页面查询',
        success: function (res) {
          if (res.confirm) {
            wx.navigateBack({
              delta: 1
            })
          } else {
            wx.navigateBack({
              delta: 1
            })
          }
        }
      })
    }
    var datacall2 = function (res) {
      console.log(res);
      if (res.data.code == 0 && res.data.data.orderStatus == "03") {
        wx.hideLoading()
        clearInterval(that.data.interval);
        wx.showModal({
          title: '',
          content: '收款成功!',
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })
            } else {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
        that.setData({
          success: 1
        })
        return;
      } else {
        if (res.data.code == 0 && res.data.data.orderStatus == "04") {
          wx.hideLoading()
          wx.showModal({
            title: '',
            content: '交易失败，请重试！',
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              } else {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        }

      }

    }
    that.getdata2("/pay/query", data, datacall2,1)
  },
  lunxun2: function (tradeno) {
    var that = this;
    var data = {};
    data.paySn = tradeno;
    data.WaitTime = 10;
    var datacall2 = function (res) {
      lunnums++;
      console.log(res);
      if (res.data.data && res.data.data == "Y") {
        wx.hideLoading()
        wx.showModal({
          title: '',
          content: '收款成功!',
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })
            } else {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
        that.setData({
          success: 1
        })
        return;
      } else {
        if (res.data.data && res.data.data == "D") {
          wx.hideLoading()
          wx.showModal({
            title: '',
            content: '条码过期失效，请重试！',
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              } else {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        } else {
          if (lunnums > 6) {
            wx.hideLoading()
            wx.showModal({
              title: '',
              content: '暂无结果，请去账单页面查询',
              success: function (res) {
                if (res.confirm) {
                  wx.navigateBack({
                    delta: 1
                  })
                } else {
                  wx.navigateBack({
                    delta: 1
                  })
                }
              }
            })
          } else {
            lunnums++;
            that.lunxun(accid, tradeno)
          }
        }

      }

    }
    that.getdata2("/pay/pay/query", data, datacall2)
  },
  recall: function (accid, tradeno, paytype) {
    var that = this;
    var data = {};

    data.subject = tradeno;
    var datacall2 = function (res) {
      console.log(res);
      if (res.data.data && res.data.data.status == 1) {
        wx.showModal({
          title: '',
          content: '收款成功!',
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })
            } else {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
        that.setData({
          success: 1
        })
        return;
      } else {
        wx.showModal({
          title: '',
          content: '暂无结果，请去账单页面查询',
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })
            } else {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      }

    }
    that.getdata2("/MsgBroker/message1/get", data, datacall2)
  }
})